package com.example.cadmin.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.cadmin.entity.DeptCabinet;
import com.example.cadmin.entity.Host;
import com.example.cadmin.entity.MyDept;
import com.example.cadmin.entity.MyUser;
import com.example.cadmin.mapper.DeptCabinetMapper;
import com.example.cadmin.service.HostService;
import com.example.cadmin.service.MyDeptService;
import com.example.cadmin.service.MyUserService;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门之间分配柜子 前端控制器
 * </p>
 *
 * @author zzsoft
 * @since 2024-06-01
 */
@Controller
@RequestMapping("/dept/cabinet")
public class DeptCabinetController {

    @Resource
    public DeptCabinetMapper deptCabinetMapper;
    @Resource
    public HostService hostService;
    @Resource
    public MyDeptService myDeptService;
    @Resource
    public MyUserService myUserService;

    @GetMapping("/index")
    public String index() {
        return "DeptCabinet/list";
    }

    @ResponseBody
    @GetMapping("/list")
    public Map<String, Object> List(@RequestParam(value = "deptName", required = false) String deptName,
                                    @RequestParam(value = "page") int page,
                                    @RequestParam(value = "limit") int limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        Map<String, Object> paramsMap = new HashMap<>();
        if (StrUtil.isNotEmpty(deptName)) {
            paramsMap.put("deptName", deptName);
        }
        // 手动处理分页
        int offset = (page - 1) * limit;
        paramsMap.put("page", offset);
        paramsMap.put("limit", limit);
        List<Map<String, Object>> mapList = deptCabinetMapper.listTable(paramsMap);
        map.put("count", deptCabinetMapper.listTableCount(paramsMap));
        map.put("data", mapList);
        return map;
    }

    @DeleteMapping("/remove/{id}")
    @ResponseBody
    public ResponseModel remove(@PathVariable("id") String id) {
        int result = deptCabinetMapper.deleteById(id);
        return result > 0 ? ResponseModel.success("删除成功") : ResponseModel.fail("删除失败");
    }

    /**
     * 配置柜子和部门关系
     * @param number
     * @return
     */
    @GetMapping("/add/door/count")
    @ResponseBody
    public List<Object> edit(@RequestParam(value = "number") String number) {
        if (StrUtil.isEmpty(number)) {
            return new ArrayList<>();
        }
        List<DeptCabinet> deptCabinetList = deptCabinetMapper.selectList(new QueryWrapper<DeptCabinet>().eq("number",number));
        Host host = hostService.getOne(new QueryWrapper<Host>().eq("number", number));
        List<Object> emptyList = new ArrayList<>();
        if (deptCabinetList.size() == 0) {
            for (int i = host.getStartNumber(); i <= host.getEndNumber(); i++) {
                emptyList.add(i);
            }
            return emptyList;
        }
        List<Object> occupiedList = new ArrayList<>();
        for (DeptCabinet deptCabinet : deptCabinetList) {
            for (int i = deptCabinet.getStartNumber(); i <= deptCabinet.getEndNumber(); i++) {
                occupiedList.add(i);
            }
        }
        for (int i = host.getStartNumber(); i <= host.getEndNumber(); i++) {
            if (!occupiedList.contains(i)) {
                emptyList.add(i);
            }
        }
        return emptyList;
    }

    @GetMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView("DeptCabinet/add");
        List<Host> hostList = hostService.list();
        List<String> numberList = new ArrayList<>();
        for (Host host : hostList) {
            numberList.add(host.getNumber());
        }

        List<DeptCabinet> deptCabinetList = deptCabinetMapper.selectList(null);
        for (DeptCabinet deptCabinet : deptCabinetList) {
            if (numberList.contains(deptCabinet.getNumber())) {
                int occupation = deptCabinet.getEndNumber() - deptCabinet.getStartNumber()+1;
                Host host = hostService.getOne(new QueryWrapper<Host>().eq("number", deptCabinet.getNumber()));
                int emptyCount = host.getEndNumber() - host.getStartNumber()+1;
                if (occupation == emptyCount) {
                    numberList.remove(deptCabinet.getNumber());
                }

            }
        }
        modelAndView.addObject("numberList", numberList);
        List<MyDept> myDeptList = myDeptService.list();
        modelAndView.addObject("myDeptList", myDeptList);
        List<MyUser> myUserList = myUserService.list();
        modelAndView.addObject("myUserList", myUserList);
        return modelAndView;
    }

    @PostMapping("/add/save")
    @ResponseBody
    public ResponseModel addSave(@RequestBody JSONObject data) {
        DeptCabinet deptCabinet = new DeptCabinet();
        deptCabinet.setDeptId(data.getInt("deptId"));
        deptCabinet.setNumber(data.getStr("number"));
        deptCabinet.setStartNumber(data.getInt("startNumber"));
        deptCabinet.setEndNumber(data.getInt("endNumber"));
        deptCabinet.setUserId(data.getInt("userId"));
        int result = deptCabinetMapper.insert(deptCabinet);
        return result > 0 ? ResponseModel.success("添加成功") : ResponseModel.fail("添加失败");
    }

}
